package com.extra_archive.project.iisu.model 
{
	import com.extra_archive.framework.net.LoaderSequencer;
	import com.extra_archive.framework.net.events.LoaderEvent;
	import com.extra_archive.project.iisu.view.detail.BasicDetailComponentView;

	import flash.display.Bitmap;

	/**
	 * @class name : AssetManager
	 * @author : implict (http://extra-archive.com)
	 * @date : 2010. 4. 24.
	 * @description :
	 */
	public class ARAssetManager extends BasicAssetManager
	{
		private var _sequenceLoader : LoaderSequencer;
		
		// list bitmap assets
		private var _listImgs : Vector.<Bitmap> = new Vector.<Bitmap>();
		
		// detail swf assets
		private var _detailSwfs : Vector.<BasicDetailComponentView> = new Vector.<BasicDetailComponentView>();
		
		public function ARAssetManager(configManager : BasicConfigManager)
		{
			super(configManager);
			
			loadAsset();
		}
		
		//--------------------------------------------------------------------------
		//
		//    getter/setter
		//
		//--------------------------------------------------------------------------

		public function get listImgs() : Vector.<Bitmap>
		{
			return _listImgs;
		}
		
		public function get detailSwfs() : Vector.<BasicDetailComponentView>
		{
			return _detailSwfs;
		}
		
		//--------------------------------------------------------------------------
		//
		//    Public Methods
		//
		//--------------------------------------------------------------------------
		
		override public function toString() : String 
		{
			return "com.extra_archive.project.iisu.model.AssetManager";
		}
		
		//--------------------------------------------------------------------------
		//
		//    Event Listeners
		//
		//--------------------------------------------------------------------------

		private function onLoadComplete(event : LoaderEvent) : void 
		{
			var id : uint = uint(event.fileName.slice(-1));
			
			if (event.fileName.slice(0, 7) == "listImg")
			{
				var imgAsset: Bitmap = event.loader.content as Bitmap;
				_listImgs[id] = imgAsset;
			}

			if (event.fileName.slice(0, 9) == "detailSwf")
			{
				var swfAsset: BasicDetailComponentView = event.loader.content as BasicDetailComponentView;
				_detailSwfs[id] = swfAsset;
			}
		}
		
		private function onLoadStart(event : LoaderEvent) : void 
		{
			trace(this + " : load " + event.fileName);
		}

		private function onAllLoadComplete(event : LoaderEvent) : void 
		{
			trace(this + " : onAllLoadComplete");
			
			dispatchEvent(new BasicModelEvent(BasicModelEvent.LOAD_ASSET_COMPLETE));
		}

		private function onLoadIOError(event : LoaderEvent) : void 
		{
			trace(this + " : " + event.message);
		}
		
		//--------------------------------------------------------------------------
		//
		//    Private Methods
		//
		//--------------------------------------------------------------------------
		
		override protected function loadAsset() : void 
		{
			_sequenceLoader = new LoaderSequencer();
			_sequenceLoader.addEventListener(LoaderEvent.START, onLoadStart);
			_sequenceLoader.addEventListener(LoaderEvent.ALL_LOADED, onAllLoadComplete);
			_sequenceLoader.addEventListener(LoaderEvent.COMPLETE, onLoadComplete);
			_sequenceLoader.addEventListener(LoaderEvent.ERROR, onLoadIOError);
			
			var i : uint, len : uint = ARConfigManager(_configManager).listImgUrls.length;			
			for (i; i < len; ++i)
			{
				_sequenceLoader.add(ARConfigManager(_configManager).listImgUrls[i], "listImg" + i);
			}
			
			var j : uint, len2 : uint = ARConfigManager(_configManager).detailSwfUrls.length;
			for (j; j < len2; ++j)
			{
				_sequenceLoader.add(ARConfigManager(_configManager).detailSwfUrls[j], "detailSwf" + j);
			}

			_sequenceLoader.execute();
		}
	}
}
